﻿
using ClientIPC;

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace Client
{
    class Program
    {
      
        static ClientPipe m_pClientPipe;
        static void Main(string[] args)
        {
            m_pClientPipe = new ClientPipe(".", "SQUALL_PIPE");
            m_pClientPipe.ConnectionEvent += M_pClientPipe_ConnectionEvent;
            m_pClientPipe.ReadDataEvent += PClientPipe_ReadDataEvent;
            m_pClientPipe.PipeClosedEvent += M_pClientPipe_PipeClosedEvent;
            m_pClientPipe.Connect();

            //PipeCommandPlusString pCmd = new PipeCommandPlusString("CLIENT", "CONNECT");
            //m_pClientPipe.SendCommandAsync(pCmd);
            //Thread.Sleep(3000);


            string command = "";
            while ((command = Console.ReadLine()) != "exit")
            {
                //for (int i = 0; i < 5; i++)
                //{
                //    ThreadPool.QueueUserWorkItem((o) =>
                //    {
                //        Console.WriteLine(o.ToString());
                //        var bytes = File.ReadAllBytes(@"E:\资料\新建文本文档.txt");
                //        Console.WriteLine("文件长度：" + bytes.Length);
                //        m_pClientPipe.SendCommandAsync(bytes);
                //        Console.WriteLine(DateTime.Now);
                //    },i);
                //}
                m_pClientPipe.SendBytes(Encoding.UTF8.GetBytes(command));
                //var bytes = File.ReadAllBytes(@"E:\资料\新建文本文档.txt");
                //m_pClientPipe.SendCommandAsync(bytes);
                //Console.WriteLine("文件长度：" + bytes.Length );
                //m_pClientPipe.SendCommandAsync(Encoding.UTF8.GetBytes(command));

                //if (command == "f")
                //{
                //    var bytes = File.ReadAllBytes(@"E:\资料\新建文本文档.txt");
                //    Console.WriteLine("文件长度：" + bytes.Length);
                //    m_pClientPipe.SendCommandAsync(bytes);
                //}
                //m_pClientPipe.SendCommandAsync(command);
            }

            m_pClientPipe.ReadDataEvent -= PClientPipe_ReadDataEvent;
            m_pClientPipe.PipeClosedEvent -= M_pClientPipe_PipeClosedEvent;
            m_pClientPipe.Close();
            m_pClientPipe = null;
        }

        private static void M_pClientPipe_ConnectionEvent(object sender, EventArgs e)
        {
           //连接成功
        }

        private static void M_pClientPipe_PipeClosedEvent(object sender, EventArgs e)
        {
            // wait around for server to shut us down
            Console.WriteLine("断开连接");
        }

        private static void PClientPipe_ReadDataEvent(object sender, PipeEventArgs e)
        {
            string szBytes = Encoding.Default.GetString(e.Bytes, 0, e.Length);
            Console.WriteLine("Got command from server: " + szBytes);
            //PipeCommandPlusString pCmd = serializer.Deserialize<PipeCommandPlusString>(szBytes.Trim(new char[] { '\0' }));
            //string szValue = pCmd.GetTransmittedString();

            //Console.WriteLine("Got command from server: " + pCmd.GetCommand() + "-" + pCmd.GetTransmittedString());

            //if (szValue == "CONNECTED")
            //{
            //    PipeCommandPlusString pCmdToSend = new PipeCommandPlusString("CLIENT", "DATA");
            //    m_pClientPipe.SendCommandAsync(pCmdToSend);
            //}
        }
    }
}
